-- modified from code by Remco Niemeijer 

module Prim where 

import List
	 
minim :: (Ord b) => (a -> b) -> [a] -> a
minim f = head . (sortBy (\ x y -> compare (f x) (f y)))

prim :: (Eq a, Ord b) => [(a, a, b)] -> [(a, a, b)]
prim es = f [(\(v,_,_) -> v) $ head es] []
  where
    f vs t = if null r 
             then t 
             else f (union vs [x,y]) (m:t)
      where
        r = filter (\(a,b,_) -> elem a vs /= elem b vs) es
        m@(x,y,_) = minim (\(_,_,c) -> c) r

main :: IO ()
main = print $ prim [('A', 'D',  5), ('A', 'B', 7), ('B', 'D', 9)
                    ,('B', 'C',  8), ('C', 'E', 5), ('B', 'E', 7)
                    ,('D', 'E', 15), ('D', 'F', 6), ('E', 'F', 8)
                    ,('F', 'G', 11), ('E', 'G', 9)]
